package com.friend.joey.applicationfriend.ChildActivities;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.friend.joey.applicationfriend.Adapters.MsgAdapter;
import com.friend.joey.applicationfriend.Classes.Msgtalk;
import com.friend.joey.applicationfriend.Database.MyDatabaseHelper;
import com.friend.joey.applicationfriend.MainActivity;
import com.friend.joey.applicationfriend.R;

import java.util.ArrayList;


/**
 * Created by Joey on 2017/5/31.
 */

public class ChildTalkActivity extends AppCompatActivity {
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;
    private ArrayList<Msgtalk> msgList = new ArrayList<>();//聊天消息
    private ImageButton button_send;
    private TextView textInput;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child_talk);
        msgRecyclerView=(RecyclerView)findViewById(R.id.ctalk_recycler_view);
        initMsgs2();
        LinearLayoutManager layoutManager = new LinearLayoutManager(ChildTalkActivity.this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList,MsgAdapter.OLDMAN);
        msgRecyclerView.setAdapter(adapter);
        msgRecyclerView.scrollToPosition(msgList.size()-1);//定位到最后一行

        button_send=(ImageButton)findViewById(R.id.button_ctsend);
        textInput=(TextView)findViewById(R.id.text_ct);

        button_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String sendtxt=textInput.getText().toString();
                //更新对话框显示
                if(!"".equals(sendtxt)) {

                    Msgtalk smsg = new Msgtalk(sendtxt, Msgtalk.TYPE_CHILD);
                    msgList.add(smsg);
                    adapter.notifyItemChanged(msgList.size()-1);//有新消息时刷新显示
                    msgRecyclerView.scrollToPosition(msgList.size()-1);//定位到最后一行
                    textInput.setText("");//清空输入框的内容
                    //添加进数据库
                    insertIntoFamilyDatabase(smsg);
                }
                else{
                    Toast.makeText(ChildTalkActivity.this, "空信息无法发送", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void initMsgs(){
        Msgtalk msg1= new Msgtalk("吃饭了吗",Msgtalk.TYPE_CHILD);
        msgList.add(msg1);
        Msgtalk msg2= new Msgtalk("吃过了",Msgtalk.TYPE_OLDMAN);
        msgList.add(msg2);
    }
    private void initMsgs2(){
        Msgtalk rem0;
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(ChildTalkActivity.this,"TestTalk.db",null,2);//数据库
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("talkchild",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            Toast.makeText(ChildTalkActivity.this, "获取到信息", Toast.LENGTH_SHORT).show();
            while(cursor.moveToNext())
            {
                String text= cursor.getString(cursor.getColumnIndex("txt"));
                int side=cursor.getInt(cursor.getColumnIndex("side"));
                rem0=new Msgtalk(text,side);
                msgList.add(rem0);
            }
        }
        else{
            Msgtalk msg1= new Msgtalk("吃饭了吗",Msgtalk.TYPE_CHILD);
            msgList.add(msg1);
            Msgtalk msg2= new Msgtalk("吃过了",Msgtalk.TYPE_OLDMAN);
            msgList.add(msg2);
            Toast.makeText(ChildTalkActivity.this, "数据库未获取到信息", Toast.LENGTH_SHORT).show();
        }
    }
    private void insertIntoFamilyDatabase(Msgtalk smsg){
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(ChildTalkActivity.this,"TestTalk.db",null,2);//数据库
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("txt",smsg.getContent());
        values.put("side",smsg.getType());
        long i= db.insert("talkchild",null,values);
        Toast.makeText(ChildTalkActivity.this,"添加家庭消息："+Long.toString(i), Toast.LENGTH_SHORT).show();
        db.close();
    }
}
